package Com.Ring.TradingMobile.Lib;

import Com.Ring.TradingMobile.HistoryActivity;
import Com.Ring.TradingMobile.Object.CompanyInfo;
import Com.Ring.TradingMobile.SQLite.DBManager;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.Filterable;
import android.widget.TextView;

public class CompanyListAdapter extends CursorAdapter implements Filterable {
	/**
	 * 
	 */
	private Activity activity;
	private Context mContent;
	private int mLayout;
	private String TAGMain = "CompanyListAdapter";
	private DBManager dbData;
    public CompanyListAdapter(Activity activity,String tag, Context context, int layout,Cursor c) {
        super(context, c);
        this.activity = activity;
        //mContent = context.getContentResolver();
        this.mContent = context;
        this.mLayout = layout;
        this.TAGMain +=("-"+tag);
        dbData = new DBManager(activity);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        /*final LayoutInflater inflater = LayoutInflater.from(context);
        final TextView view = (TextView) inflater.inflate(
                R.layout.two_line_list_item, parent, false);
        view.setText(cursor.getString(5));
        return view;
        */
    	//Cursor c = getCursor();
    	final LayoutInflater inflater = LayoutInflater.from(context);
    	View view = inflater.inflate(this.mLayout, parent,false);
    	String code = cursor.getString(cursor.getColumnIndex(CompanyInfo.CODE));
    	String fullName = cursor.getString(cursor.getColumnIndex(CompanyInfo.FULLNAME));
    	TextView text1 = (TextView)view.findViewById(android.R.id.text1);
    	TextView text2 = (TextView)view.findViewById(android.R.id.text2);
    	if(text1!=null) {
    		text1.setText(code);
    	}
    	if(text2!=null) {
    		text2.setText(fullName);
    	}
    	return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    	String code = cursor.getString(cursor.getColumnIndex(CompanyInfo.CODE));
    	String fullName = cursor.getString(cursor.getColumnIndex(CompanyInfo.FULLNAME));
    	TextView text1 = (TextView)view.findViewById(android.R.id.text1);
    	TextView text2 = (TextView)view.findViewById(android.R.id.text2);
    	if(text1!=null) {
    		text1.setText(code);
    	}
    	if(text2!=null) {
    		text2.setText(fullName);
    	}
    }

    @Override
    public String convertToString(Cursor cursor) {
        return cursor.getString(cursor.getColumnIndex(CompanyInfo.CODE));
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }

        if(constraint==null || constraint=="")
        	return null;
		String sql = "select * from "
				+ CompanyInfo.TABLE_NAME +" where "+CompanyInfo.CODE+" like '"+constraint+"%' order by "+CompanyInfo.CODE;
		//Log.d(TAGMain,sql);
		Cursor cur = dbData.Select(sql);
		if(cur!=null)
			this.activity.startManagingCursor(cur);
        return cur;
    }
}